package com.hxzy.module.store.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.web.resp.R;
import com.hxzy.module.store.req.store.StoreListReq;
import com.hxzy.module.store.resp.store.StoreListResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@Api(tags = "门店管理")
@RequestMapping("/storeManage")
public class StoreController {

    private static List<StoreListResp>  allStore=new ArrayList<>();

    static{
        StoreListResp resp01=new StoreListResp();
        resp01.setStoreId("1001");
        resp01.setStoreName("光电园门店");

        allStore.add(resp01);

        StoreListResp resp02=new StoreListResp();
        resp02.setStoreId("2001");
        resp02.setStoreName("互联网学院门店");
        allStore.add(resp02);
    }


    @ApiOperation(value = "门店列表", notes = "门店列表")
    @GetMapping(value = "/storeList")
    public R<StoreListResp> storeList(@Validated  StoreListReq req) {

        return R.okHasData(allStore.get(0));
    }

    @ApiOperation(value = "根据主键查询门店")
    @SentinelResource(value = "/storeManage/{id}")
    @GetMapping(value = "/{id}")
    public R<StoreListResp> findById(@PathVariable(value = "id") String id){
        Optional<StoreListResp> first = allStore.stream().filter(p -> p.getStoreId().equals(id)).findFirst();

        return first.map(R::okHasData).orElseGet(() -> R.build(AckCode.NOT_FOUND_DATA));
    }

    @ApiOperation(value = "新增门店")
    @PostMapping
    public R<Boolean>  addData(StoreListResp req){
        allStore.add(req);
        return R.ok();
    }


}
